﻿/****************************************************************************
Copyright (c) 2014-2015 凌惊雪 微信:Lingjingxue 邮箱:lingjingxue@sina.com QQ:271487457

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/

using UtilLib;
using static ServerBase.Service.BaseDispatch;
using ServerBase.Service;
using UtilPublic.ServerLoger;
using ServerBase.Protocol;
using ServerGame.Manager;
using System;
using System.Threading;
using System.Xml.Serialization;
using UtilPublic.Serialization;
using ServerPublic.Data;
using ServerGame.GameObject;
using ServerBase.Config;
using ServerPublic.DataBaseMysql;
using ServerGame.Data;
using ServerBase.LunarSocket;

namespace ServerGame.Service
{
    /// <summary>
    /// 消息分发器
    /// </summary>
    public static class Dispatcher
    {
        /// <summary>
        /// 消息注册器即时
        /// </summary>
        public static void RegisterEventHandlerAll()
        {
            BaseDispatch.DispatchApiRegister(DispatcherMsg.Instance);

            RegisterEventHandlerCheck();
        }
        //检查
        public static void RegisterEventHandlerCheck()
        {
            foreach (EProtocolId pid in Enum.GetValues(typeof(EProtocolId)))
            {
                var spid = pid.ToString();
                if (spid.StartsWith("C2G"))
                {
                    if (!(DictProtocolEvent.ContainsKey(pid) || DictProtocolEventImm.ContainsKey(pid)))
                    {
                        loger.Warn($"未注册 {pid}");
                    }
                }
            }
        }

        // 发送消息(通过Player)
        public static void Send<T>(this Player player, T objMsg) where T : ProtocolMsgBase
        {
            player.Session.Send(objMsg);
        }
        // 发送消息(通过Player唯一ID)
        public static void Send<T>(long Puid, T objMsg) where T : ProtocolMsgBase
        {
            if (DataGame.GetPlayer(Puid, out var player))
            {
                var session = player.Session;
                session.Send(objMsg);
            }
        }
        // 广播消息给所有玩家
        public static void Boardcast<T>(T objMsg) where T : ProtocolMsgBase
        {
            foreach (var player in DataGame.DictPlayerOnline.Values)
            {
                var session = player.Session;
                session.Send(objMsg);
            }
        }
    }
}

namespace ServerGame.Manager
{
    public partial class DispatcherMsg
    {
        #region 单例
        private static readonly DispatcherMsg _instance = new DispatcherMsg();
        public static DispatcherMsg Instance { get { return _instance; } }
        private DispatcherMsg() { } //私有构造方法(防止被外部实例化)
        #endregion


    }
}